import { fileURLToPath, URL } from 'node:url'
import { UserConfig, ConfigEnv, defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import * as path from 'path'

// https://vite.dev/config/
export default defineConfig(({ mode, command }: ConfigEnv): UserConfig => {
  const env = loadEnv(mode, process.cwd())
  return {
    plugins: [
      vue(),
      vueDevTools(),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), 'src/views/ProcessDesign/bpmn-icons')],
        // 指定symbolId格式
        symbolId: '[name]',
        customDomId: '__svg__icons__dom__',
        svgoOptions: command === 'build',
      }),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },
    // 添加开发服务器配置
    server: {
      port: 3000,
      proxy: {
        // 配置代理
        [env.VITE_API_BASE_URL]: {
          target: 'http://localhost:8080', // 后端服务器地址
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp('^' + env.VITE_API_BASE_URL), ''),
        },
      },
    },
  }
})
